﻿//==============================================================================
// File: Property.cs
// Created: 2010-05-10
// Author: Piotr Włodek
//==============================================================================
// This file is a part of MAMMOTH project.
// Copyright (C) 2010 AGH University of Science and Technology, Krakow.
// https://caribou.iisg.agh.edu.pl/trac/mammoth
//
// Mammoth is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Mammoth is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with WpfRcp. If not, see http://www.gnu.org/licenses/.
//==============================================================================

using System;
using System.Diagnostics;

namespace WpfRcp.PropertyModel.Core
{
    /// <summary>
    /// Default implementation of <see cref="IProperty"/> interface.
    /// </summary>
    [DebuggerDisplay("Name = {MetaProperty.Name}, Value = {Value}, IsValid = {IsValid}, IsSynchronizing = {IsSynchronizing}")]
    [Serializable]
    public class Property : IProperty, ISynchronizable
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Property"/> class.
        /// </summary>
        /// <param name="property"><see cref="MetaProperty"/> which represents the property itself.</param>
        public Property(MetaProperty property)
        {
            if (property == null)
                throw new ArgumentNullException("property");

            MetaProperty = property;
        }

        /// <summary>
        /// Gets a <see cref="IProperty.MetaProperty"/> instance identifying this value.
        /// </summary>
        public MetaProperty MetaProperty { get; private set; }

        /// <summary>
        /// Gets or sets the value indicating whether the value is being synchronized.
        /// </summary>
        public bool IsSynchronizing { get; set; }

        /// <summary>
        /// Gets or sets a value.
        /// </summary>
        public virtual object Value { get; set; }

        #region IDisposable Implementation

        [NonSerialized]
        private bool m_Disposed;

        /// <summary>
        /// Returns true if object has been disposed.
        /// </summary>
        protected bool Disposed
        {
            get
            {
                lock (this)
                {
                    return m_Disposed;
                }
            }
        }

        /// <summary>
        /// Performs cleaning. Always call base class's <see cref="Dispose(bool)"/> method.
        /// </summary>
        /// <param name="disposing">True is this method is called via <see cref="Dispose"/> method.
        /// False indicates that the method has been called from destructor.</param>
        /// <remarks>
        /// Generally, you should dispose both managed and unmanaged resources when <paramref name="disposing"/>
        /// is true, and only unmanaged resources when <paramref name="disposing"/> is false.
        /// </remarks>
        protected virtual void Dispose(bool disposing)
        {
        }

        public void Dispose()
        {
            lock (this)
            {
                if (m_Disposed == false)
                {
                    Dispose(true);
                    m_Disposed = true;

                    GC.SuppressFinalize(this);
                }
            }
        }

        /// <summary>
        /// Destructor will run only if <see cref="Dispose"/> wasn't called.
        /// Do not provide descructors in derived types. Use <see cref="Dispose(bool)"/> instead.
        /// </summary>
        ~Property()
        {
            Dispose(false);
        }

        #endregion
    }
}